package com.asset.account.biz.impl;

import com.asset.account.biz.IAccountBiz;
import com.asset.account.dao.entity.AccountEntity;
import com.asset.account.dao.mapper.IAccountMapper;
import com.asset.account.form.AccountForm;
import com.asset.common.enums.EnumTradingDirectionType;
import com.asset.common.enums.ResultCodeEnum;
import com.asset.common.helper.CurrentUserHelper;
import com.asset.common.result.ResultMsg;
import com.asset.common.utils.JsonUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class AccountBizImpl implements IAccountBiz {

    @Resource
    private IAccountMapper accountMapper;

    @Override
    public ResultMsg initUserAccount(AccountForm accountForm) {
        ResultMsg resultMsg = new ResultMsg();
        if (accountForm == null) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("参数异常");
            return resultMsg;
        }
        System.out.println("resultMsg = " + JsonUtil.toJson(accountForm));
        Long currentUserId = CurrentUserHelper.getCurrentUserId();
        AccountEntity accountEntity = conver2Entity(accountForm, currentUserId);
        try {
            if (accountEntity.getId() != null) {
                accountMapper.update(accountEntity);
            } else {
                accountMapper.insert(accountEntity);
            }

        } catch (Exception e) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("账户初始化失败！请重试");
            return resultMsg;
        }
        resultMsg.setCode(ResultCodeEnum.SUCCESS.getCode());
        resultMsg.setMsg("账户初始化成功");
        return resultMsg;
    }

    @Override
    public ResultMsg update(AccountForm accountForm) {
        ResultMsg resultMsg = new ResultMsg();
        Long currentUserId = CurrentUserHelper.getCurrentUserId();
        if (accountForm == null || accountForm.getId() == null) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("参数异常");
            return resultMsg;
        }
        try {
            AccountEntity accountEntity = conver2Entity(accountForm, currentUserId);
            accountMapper.update(accountEntity);
        } catch (Exception e) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("修改失败！请重试");
            return resultMsg;
        }
        resultMsg.setCode(ResultCodeEnum.SUCCESS.getCode());
        resultMsg.setMsg("修改成功！");
        return resultMsg;
    }

    @Override
    public AccountForm getUserAccount() {
        Long currentUserId = CurrentUserHelper.getCurrentUserId();
        return getByUserId(currentUserId);
    }

    @Override
    public AccountForm getByUserId(Long userId) {
        AccountEntity entity = accountMapper.getByUserId(userId);
        AccountForm accountForm = conver2Form(entity);
        return accountForm;
    }

    @Override
    public void synchronizeUserAccount(Long userAccountId, BigDecimal transactionAmount, String tradingDirectionType) {
        AccountEntity accountEntity = getById(userAccountId);
        AccountEntity updateEntity = new AccountEntity();
        updateEntity.setId(accountEntity.getId());
        if(EnumTradingDirectionType.INCOME.getCode().equals(tradingDirectionType)){
            //收入  总收入加  余额加
            updateEntity.setIncome(accountEntity.getIncome().add(transactionAmount));
            updateEntity.setTotalAmount(accountEntity.getTotalAmount().add(transactionAmount));
        }else if(EnumTradingDirectionType.PAY.getCode().equals(tradingDirectionType)){
            //支出  总支出加  余额减
            updateEntity.setExpenditure(accountEntity.getExpenditure().add(transactionAmount));
            updateEntity.setTotalAmount(accountEntity.getTotalAmount().subtract(transactionAmount));
        }
        accountMapper.update(updateEntity);
    }

    @Override
    public void synchronizeUserAccountReBack(Long userAccountId, BigDecimal transactionAmount, String tradingDirectionType) {
        AccountEntity accountEntity = getById(userAccountId);
        AccountEntity updateEntity = new AccountEntity();
        updateEntity.setId(accountEntity.getId());
        if(EnumTradingDirectionType.INCOME.getCode().equals(tradingDirectionType)){
            //收入  总收入加  余额加
            updateEntity.setIncome(accountEntity.getIncome().subtract(transactionAmount));
            updateEntity.setTotalAmount(accountEntity.getTotalAmount().subtract(transactionAmount));
        }else if(EnumTradingDirectionType.PAY.getCode().equals(tradingDirectionType)){
            //支出  总支出加  余额减
            updateEntity.setExpenditure(accountEntity.getExpenditure().subtract(transactionAmount));
            updateEntity.setTotalAmount(accountEntity.getTotalAmount().add(transactionAmount));
        }
        accountMapper.update(updateEntity);
    }

    public AccountEntity getById(Long userAccountId){
        return accountMapper.getById(userAccountId);
    }

    private AccountForm conver2Form(AccountEntity entity) {
        if (entity == null) {
            return null;
        }
        AccountForm form = new AccountForm();
        if(entity.getId() != null){
            form.setId(entity.getId());
        }
        if (entity.getUserId() != null) {
            form.setUserId(entity.getUserId());
        }
        if (entity.getIncome() != null) {
            form.setIncome(entity.getIncome());
        }
        if (entity.getExpenditure() != null) {
            form.setExpenditure(entity.getExpenditure());
        }
        if (entity.getTotalAmount() != null) {
            form.setMount(entity.getTotalAmount());
        }
        return form;
    }

    private AccountEntity conver2Entity(AccountForm accountForm, Long userId) {
        if (accountForm == null) {
            return null;
        }
        AccountEntity entity = new AccountEntity();
        if (accountForm.getId() != null) {
            entity.setId(accountForm.getId());
        }
        if (userId != null) {
            entity.setUserId(userId);
        }
        if (accountForm.getIncome() != null) {
            entity.setIncome(accountForm.getIncome());
        }
        if (accountForm.getExpenditure() != null) {
            entity.setExpenditure(accountForm.getExpenditure());
        }
        if (accountForm.getMount() != null) {
            entity.setTotalAmount(accountForm.getMount());
        }
        return entity;
    }
}
